﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Controls;
using DALHelper.Business;
using System.Windows;

namespace ResortManager.Wizard
{
    /// <summary>
    /// Interaction logic for DefaultProgress.xaml
    /// </summary>
    public partial class CIProgress : UserControl, IProceedureProcessComponent
    {
        private List<ProceedureCompleteDelegate> proceedureCompleteDelegates = new List<ProceedureCompleteDelegate>();
        private int counter = 0;
        private delegate void RefreshDelegate();
        private Thread t = null;

        public CIProgress()
        {
            InitializeComponent();
        }

        private void updateProgressLoop()
        {
            try
            {
                BusinessManager busManager = ((App)App.Current).BUSManager;

                counter = 40;
                Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new RefreshDelegate(updateProgress));

                busManager.Room.CheckIn(((Transaction)ParentWizard.WizardCommonData).TransactionID);
               
                counter = 100;
                Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new RefreshDelegate(updateProgress));
                Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new RefreshDelegate(updateFinished));
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi khi nhận phòng.\r\nVui lòng xem lại các thông tin và thử lại.\r\n\r\n" + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                counter = 0;
                Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new RefreshDelegate(updateProgress));

                Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new RefreshDelegate(rollBack));
            }
        }

        private void rollBack()
        {
            if (ParentWizard != null)
            {
                ParentWizard.Back();
                ParentWizard.Back();
            }
        }

        private void updateProgress()
        {
            progressBar.Value = counter;
        }

        private void updateFinished()
        {
            for (int i = 0; i < proceedureCompleteDelegates.Count; i++)
            {
                proceedureCompleteDelegates[i].Invoke();
            }
        }

        #region IProceedureProcessComponent Members

        public List<ProceedureCompleteDelegate> ProceedureCompleteDelegates
        {
            get { return proceedureCompleteDelegates; }
        }

        public void startProceedure()
        {
            t = new Thread(new ThreadStart(updateProgressLoop));
            t.Name = "Default Master Progress Thread";
            t.Start();
        }

        public bool cancelProceedure()
        {
            try
            {
                //Thread.Sleep(2000);
            }
            catch (Exception)
            {
            }

            try
            {
                t.Abort();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        #endregion

        #region IProceedureComponent Members

        public string KeyText
        {
            get { return "Thực hiện"; }
        }

        public bool RequestNext()
        {
            return true;
        }

        public void UpdateComponent()
        {

        }

        public ProceedureDialog ParentWizard
        {
            get;
            set;
        }

        #endregion
    }
}
